<template>
  <div>
    <el-card class="search-card">
      <BasicForm
        ref="formData"
        defaultExpandStatus
        :formColumns="formItems"
        :dataSource="formData"
        :handleSearchForm="handleQueryForm"
        @getFormData="getFormData"
      />
    </el-card>
    <el-card class="table-card">
      <SkusTable
        ref="skusTable"
        showActionCol
        showPagination
        :tableHeight="480"
        :defaultMinWidth="100"
        :dataSource="TableInfo.list"
        :columns="TableInfo.columns"
        :loading="TableInfo.loading"
        :pageSizes="pageSizes"
        :pagination="pagination"
        :total="pagination.total"
        :actionRender="renderTableBtnAction"
        @pageChange="handlePageChange"
        @pageSizeSelect="handlePageSizeChange"
      />
    </el-card>
  </div>
</template>
<script>
import BasicForm from '@/components/BasicForm';
import SkusTable from '@/components/skusTable';
import {
  AUDIT_FORM_ITEMS,
  AUDIT_TABLE_COLUMNS,
  AUDIT_STATE_VALUES,
  COMMIT_TYPES
} from '@/constants/markupRate';
import { getInitFormData, TableInfo } from '@/utils/getUtils';
import { getTableAction } from '@/utils/getBtnsRender';
import { setAuthBtnId } from '@/utils/bizUtils';
import skusPage from '@/mixins/commodity/skusPage';
import { getProductMarkupRateRecordsService } from '@/api/basics/categoryManage/guideMarkupRateToAudit';
import { queryOAUrlService } from '@/api/public';
export default {
  mixins: [skusPage],
  components: {
    BasicForm,
    SkusTable,
  },
  data () {
    return {
      formItems: AUDIT_FORM_ITEMS,
      formData: getInitFormData(AUDIT_FORM_ITEMS),
      TableInfo: new TableInfo({ columns: AUDIT_TABLE_COLUMNS }),
      pageSizes: [10, 100, 1000],
    }
  },
  mounted () {
    this.getTableData()
  },
  methods: {
    // 渲染表格操作栏按钮
    renderTableBtnAction (h, { row }) {
      const viewBtn = { label: '查看', method: this.handleView }
      const auditBtn = { label: '审核', method: this.handleAudit, hasAuth: true }
      const auditRecordBtn = { label: '审批记录', method: this.handleAuditRecord }
      const actionMaps = {
        [AUDIT_STATE_VALUES.Commit]: [viewBtn, auditBtn], // 审核中
        [AUDIT_STATE_VALUES.Ok]: [viewBtn, auditRecordBtn], // 审核通过
        [AUDIT_STATE_VALUES.Fail]: [viewBtn, auditRecordBtn], // 审核未通过
      }
      const actionList = actionMaps[row.commitState] ?? []
      return getTableAction({ h, row, actionList })
    },
    // 获取表格数据
    async getTableData () {
      setAuthBtnId('查询')
      const data = {
        ...this.formData,
        ...this.paginationParams,
        commitType: COMMIT_TYPES.Group // 集团指导加价率commitType
      }
      const res = await getProductMarkupRateRecordsService(data, this.TableInfo.setLoading)
      if (res?.retData) {
        this.TableInfo.setList(res.retData.results)
        this.setTotalNum(res.retData.totalNum)
      }
    },
    // 查看
    handleView (row) {
      const { href } = this.$router.resolve({
        name: 'guideMarkupRateToAuditView',
        query: { recordNo: row.recordNo },
      })
      window.open(href, '_blank')
    },
    // 审核
    handleAudit (row) {
      this.redirectOA(row.workflowRequestId)
    },
    // 审批记录
    handleAuditRecord (row) {
      this.redirectOA(row.workflowRequestId)
    },
    // 跳转OA
    async redirectOA (oaRequestId) {
      const data = { oaRequestId }
      const res = await queryOAUrlService(data, this.TableInfo.setLoading)
      if (res?.retData) window.open(res.retData)
    }
  }
}
</script>
